﻿#関連情報サイト
#http://arcanecode.com/2016/04/05/accessing-a-powershell-class-defined-in-a-module-from-outside-a-module/
#https://www.automatedops.com/blog/2016/01/28/testing-powershell-classes/
using module .\UnitTest.psm1

class TestTarget
{
    [int]Add([int]$a,[int]$b)
    {
        Write-host "TestTarget:Add"
        return $a + $b;
    }
}

class TestSample : TestBase
{
    TestSample()
    {
        Write-host "TestSample:TestSample"
        $this.target = [TestTarget]::new();
    }

    [SetUp()]
    [void]SetUp()
    {
        Write-host "TestSample:SetUp"
        $this.target = [TestTarget]::new();
    }

    [TearDown()]
    [void]TearDown()
    {
        Write-host "TestSample:TearDown"
        $this.target = $null;
    }

    [Test()]
    [void]足し算テスト()
    {
        Write-host "TestSample:TestAdd1"
        $this.AssertEquals( 3, $this.target.Add(1,2), "Error TestAdd1" );
    }
    
    [Test()]   
    [void]その他の足し算テスト()
    {
        Write-host "TestSample:TestAdd3"
        $this.Assert( $this.target.Add(0,0) -eq 0, "Error Assertでチェック" );
        $this.Assert( $this.target.Add(1,-1) -eq 0 );
    }

    [Test()]
    [void]AssertEquals_エラー()
    {
        Write-host "AssertEquals_エラー"
        $this.AssertEquals( 1, $this.target.Add(2,0), "Error AssertEquals" );
    }

    [Test()]
    [void]Assert_エラー()
    {
        Write-host "Assert_エラー"
        $this.Assert( ( 1 -eq $this.target.Add(2,0)) , "Error Assert" );
    }

    [Test()]
    [void]例外_エラー()
    {
        Write-host "例外_エラー"

        $zero = 0
        $val  = 1
        $ans = $val / $zero
        Write-Host $ans
    }

}

$error.Clear()
[void](([TestSample]::new()).RunTest());
#スクリプトのエラー処理は、ここで行う必要がある
if( ( $error -ne $null ) -and ( $error.Count -ne 0 ) )
{
    $errFlag = $false
    foreach($err in $error)
    {
        if( $err.Exception.GetType().Name -ne 'UnitTestException' )
        {
            if( $errFlag -eq $false )
            {
                $errFlag = $true
                Write-host "`n*****Error info*****"
            }
            $err
        }
    }
}
#>